package com.graduation.roombooking.controller.applet;

import com.graduation.roombooking.service.IEquipmentService;
import com.graduation.roombooking.service.IOrderService;
import com.graduation.roombooking.service.UserService;
import com.graduation.roombooking.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author:CaiShuangLian
 * @FileName:
 * @Date:Created in  2022/5/5 16:03
 * @Version:
 * @Description:TODO
 */
@RestController
@RequestMapping("/applet/order")
@Api(tags = "Applet——订单模块")
public class AppletOrderController {
    @Autowired
    private UserService userService;
    @Autowired
    private IOrderService orderService;
    @Autowired
    private IEquipmentService equipmentService;

    @ApiOperation("预约")
    @ResponseBody
    @PostMapping("/appoint")
    @Transactional(rollbackFor = Exception.class)
    public RespBean appoint(@RequestBody AppointRoomVo appointRoomVo, HttpServletRequest request, HttpServletResponse response){
        String ticket=appointRoomVo.getTicket();
        if(ticket!=null && !ticket.isEmpty()){
            //根据cookie获取用户信息
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(userVo!=null ){
                //获取用户手机号
                Long phone=userVo.getUserPhone();
                return orderService.appoint(appointRoomVo,phone);
            }
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }

    @ApiOperation("设备选项（返回设备id，设备名和设备数量）")
    @ResponseBody
    @PostMapping("/equipment/multiple/choice")
    public RespBean getEquipmentMultipleChoice(@RequestBody EquipmentMultipleChoice equipmentMultipleChoice, HttpServletRequest request, HttpServletResponse response){
        String ticket=equipmentMultipleChoice.getTicket();
        if(ticket!=null && !ticket.isEmpty()){
            //根据cookie获取用户信息
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(userVo!=null ){
//                //获取用户手机号
//                Long phone=userVo.getUserPhone();
//                return orderService.appoint(appointRoomVo,phone);
                return equipmentService.getEquipmentMultipleChoice(equipmentMultipleChoice.getCompanyName());

            }
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }

    @ApiOperation("1、可预约时间")
    @ResponseBody
    @PostMapping("/time")
    public RespBean getTimeList(@RequestBody Integer roomId,  HttpServletRequest request, HttpServletResponse response){
       return orderService.getTimeList(roomId);
    }

    @ApiOperation("取消订单(根据orderId 取消订单)")
    @ResponseBody
    @PostMapping("/cancel")
    public RespBean cancelOrder(@RequestBody CancelVo cancelVo,HttpServletRequest request, HttpServletResponse response ){
        String ticket=cancelVo.getTicket();
        if(ticket!=null && !ticket.isEmpty()){
            //根据cookie获取用户信息
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(userVo!=null ){
                //获取用户的手机号
                Long phone=userVo.getUserPhone();
                return orderService.cancelOrder(cancelVo.getOrderId(),phone);

            }
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }

    @ApiOperation("获取我的订单")
    @ResponseBody
    @PostMapping("/personal")
    public RespBean getPersonalOrder(@RequestBody PersonalOrderVo personalOrderVo,HttpServletRequest request, HttpServletResponse response){
        String ticket=personalOrderVo.getTicket();
        if(ticket!=null && !ticket.isEmpty()){
            //根据cookie获取用户信息
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(userVo!=null ){
                //获取用户的手机号
                Long phone=userVo.getUserPhone();
                return orderService.getPersonalOrder(personalOrderVo,phone);

            }
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }

}
